package com.net.shop.car.manager.ui.personalcenter;

import java.math.BigDecimal;
import java.util.ArrayList;

import android.content.Context;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.net.shop.car.manager.Constant;
import com.net.shop.car.manager.R;
import com.net.shop.car.manager.api.model.ChongZhi;
import com.net.shop.car.manager.api.model.Goods;
import com.net.shop.car.manager.api.volley.VolleyCenter;
import com.net.shop.car.manager.api.volley.VolleyListenerImpl;
import com.net.shop.car.manager.api.volley.request.VbListGuize;
import com.net.shop.car.manager.api.volley.response.VbListGuizeResponse;
import com.net.shop.car.manager.ui.FullScreenDialog;
import com.net.shop.car.manager.ui.view.MyListView;
import com.net.shop.car.manager.utils.StringUtils;
import com.net.shop.car.manager.utils.payment.PaymentUtils;
import com.net.shop.car.manager.utils.payment.Product;

public class ChongZhiDialog extends FullScreenDialog {
	private TextView my_jifen;
	private MyListView chongzhi_list;
	private EditText chongzhi_money;
	private TextView titleView;
	private ImageView title_back_btn;
	private String myJifen;
	private ArrayList<ChongZhi> guizeList;
	private int checkedItem;
	private ChongZhiAdapter adapter;
	private TextView jiesuan_mony;
	private TextView jiesuan_pay_btn;
	private boolean isListClicked;

	public ChongZhiDialog(Context context, String myJifen) {
		super(context, R.style.Dialog_Fullscreen);
		this.myJifen = myJifen;
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.dialog_chongzhi);
		initView();
		getListData();
	}

	private void getListData() {
		// TODO Auto-generated method stub
		guizeList = new ArrayList<ChongZhi>();
		adapter = new ChongZhiAdapter();
		chongzhi_list.setAdapter(adapter);
		VbListGuize guize = new VbListGuize(activity.getStringFromSharedPreferences(Constant.sp.user_id));
		VolleyCenter.getVolley().addPostRequest(guize, new VolleyListenerImpl<VbListGuizeResponse>(new VbListGuizeResponse()) {

			@Override
			public void deleveryResponse(VbListGuizeResponse response) {
				if (response.isSuccess()) {
					if (response.getExchangeRuleArray() != null) {
						guizeList = response.getExchangeRuleArray();
						adapter.notifyDataSetChanged();
						chongzhi_list.setAdapter(adapter);
					}
				} else {
					Toast.makeText(activity, response.getErrorMsg(), Toast.LENGTH_LONG).show();
				}
			}
		});
	}

	private void initView() {
		// TODO Auto-generated method stub
		titleView = (TextView) findViewById(R.id.title_txt);
		my_jifen = (TextView) findViewById(R.id.my_jifen);
		chongzhi_money = (EditText) findViewById(R.id.chongzhi_money);
		chongzhi_list = (MyListView) findViewById(R.id.chongzhi_list);
		titleView.setText("积分充值");
		title_back_btn = (ImageView) findViewById(R.id.title_back_btn);
		jiesuan_mony = (TextView) findViewById(R.id.jiesuan_mony);
		jiesuan_pay_btn = (TextView) findViewById(R.id.jiesuan_pay_btn);
		title_back_btn.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				dismiss();
			}
		});
		my_jifen.setText(myJifen);
		chongzhi_list.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3) {
				// TODO Auto-generated method stub
				isListClicked = true;
				guizeList.get(checkedItem).setChecked(false);
				System.out.println(checkedItem + "   222    " + guizeList.get(checkedItem).isChecked());
				checkedItem = position;
				guizeList.get(position).setChecked(true);
				System.out.println(position + "    111   " + guizeList.get(checkedItem).isChecked());
				adapter.notifyDataSetInvalidated();
				chongzhi_money.setText("");
				jiesuan_mony.setText(guizeList.get(checkedItem).getMoney());
			}
		});
		chongzhi_money.addTextChangedListener(new TextWatcher() {

			@Override
			public void onTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {
				// TODO Auto-generated method stub

			}

			@Override
			public void beforeTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {
				// TODO Auto-generated method stub

			}

			@Override
			public void afterTextChanged(Editable arg0) {
				// TODO Auto-generated method stub
				if (!isListClicked) {
					guizeList.get(checkedItem).setChecked(false);
					adapter.notifyDataSetInvalidated();
					jiesuan_mony.setText(chongzhi_money.getText().toString());
				}
				isListClicked = false;
			}
		});
		jiesuan_pay_btn.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				Product product = new Product();
				product.setOrder("961111");
				product.setSubject("chongzhi");
				product.setPrice(jiesuan_mony.getText().toString());
				PaymentUtils.getInstance().pay(product);
			}
		});
	}

	private class ChongZhiAdapter extends BaseAdapter {

		@Override
		public int getCount() {
			// TODO Auto-generated method stub
			return guizeList.size();
		}

		@Override
		public Object getItem(int arg0) {
			// TODO Auto-generated method stub
			return arg0;
		}

		@Override
		public long getItemId(int arg0) {
			// TODO Auto-generated method stub
			return arg0;
		}

		@Override
		public View getView(int position, View currentView, ViewGroup arg2) {
			// TODO Auto-generated method stub
			Holdler holdler;
			if (currentView == null) {
				currentView = LayoutInflater.from(activity).inflate(R.layout.item_chongzhi_list, null);
				holdler = new Holdler();
				holdler.checked_item = (ImageView) currentView.findViewById(R.id.checked_item);
				holdler.chong_money = (TextView) currentView.findViewById(R.id.chong_money);
				holdler.chong_jifen = (TextView) currentView.findViewById(R.id.chong_jifen);
				currentView.setTag(holdler);
			} else {
				holdler = (Holdler) currentView.getTag();
			}
			System.out.println("posistion   " + position + "  " + guizeList.get(position).isChecked());
			if (!guizeList.get(position).isChecked()) {
				holdler.checked_item.setImageResource(R.drawable.checkbox_no_check);
			} else {
				holdler.checked_item.setImageResource(R.drawable.checkbox_check);
			}
			holdler.chong_money.setText(guizeList.get(position).getMoney());
			holdler.chong_jifen.setText(String.valueOf(StringUtils.moneyToVB(StringUtils.filterMoneyNull(guizeList.get(position).getMoney()))));
			return currentView;
		}

	}

	private class Holdler {
		private ImageView checked_item;
		private TextView chong_money;
		private TextView chong_jifen;
	}

}
